package com.labichaoka.chaoka.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.entity.DistrictDataModel;
import com.labichaoka.chaoka.entity.ProvinceDataPersistModel;
import com.labichaoka.chaoka.ui.baseinfo.person.DistrictListWheelAdapter;
import com.labichaoka.chaoka.widget.wheel.OnWheelChangedListener;
import com.labichaoka.chaoka.widget.wheel.WheelView;
import com.labichaoka.chaoka.widget.wheel.adapters.ArrayWheelAdapter;
import com.labichaoka.chaoka.widget.wheel.adapters.ListWheelAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by jackwang on 16-11-22.
 */

public class DialogFactory {


    public static CustomDialog createDialog(Activity act, @LayoutRes int layout, int gravity) {
        final CustomDialog dialog = new CustomDialog(act, R.style.ActionSheetDialogStyle);
        final View dialogView = LayoutInflater.from(act).inflate(layout, null);
        dialog.setContentView(dialogView);
        final Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(gravity);
        WindowManager.LayoutParams wl = dialogWindow.getAttributes();
        wl.x = 0;
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialog.onWindowAttributesChanged(wl);
        return dialog;
    }








    public static SelectAreaDialog createSelectAreaDialog(Activity act, ProvinceDataPersistModel prov) {
        return new SelectAreaDialog(act, prov);
    }



    public static class CustomDialog extends Dialog {

        public CustomDialog(Context context) {
            super(context);
        }

        public CustomDialog(Context context, int themeResId) {
            super(context, themeResId);
        }

        protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }

        public CustomDialog setOnViewClickListener(View.OnClickListener listener, @IdRes int viewId) {
            findViewById(viewId).setOnClickListener(listener);
            return this;
        }

        public CustomDialog setOnViewClickListener(View.OnClickListener listener, @IdRes int... viewIds) {
            for (int viewId : viewIds) {
                findViewById(viewId).setOnClickListener(listener);
            }
            return this;
        }

    }


    public static class SelectAreaDialog {
        private OnSelectAreaListener mOnSelectAreaListener;

        public interface OnSelectAreaListener {
            void onSelect(String addr, String zipCode, String id);
        }

        private final OnWheelChangedListener mOnWheelChangedListener = new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                if (wheel == mProvince) {
                    updateCities();
                } else if (wheel == mCity) {
                    updateAreas();
                }
            }
        };

        private final WheelView mProvince;
        private final WheelView mCity;
        private final WheelView mArea;
        private final Map<String, List<String>> mCitisDatasMap;
        private final Map<String, List<DistrictDataModel>> mAreaDatasMap;
        private final String[] mProvinceDatas;
        private String mCurrentProviceName;
        private String mCurrentCityName;
        private DialogFactory.CustomDialog mSelectAreaDialog;
        private final Activity ownerActivity;


        public void show() {
            if (mSelectAreaDialog != null && ownerActivity.getWindow().getDecorView().getWindowToken() != null) {
                //                mProvince.setViewAdapter(new ArrayWheelAdapter<>(ownerActivity, mProvinceDatas));
                mSelectAreaDialog.show();
                updateCities();
                return;
            }
        }

        public void hide() {
            if (mSelectAreaDialog != null) {
                mSelectAreaDialog.hide();
                return;
            }
        }

        public void setOnSelectAreaListener(OnSelectAreaListener onSelectAreaListener) {
            mOnSelectAreaListener = onSelectAreaListener;
        }

        public SelectAreaDialog(Activity act, ProvinceDataPersistModel prov) {
            ownerActivity = act;

            List<String> provinceList = prov.getProvinceList();
            mCitisDatasMap = prov.getCityMap();
            mAreaDatasMap = prov.getDistrictMap();
            mProvinceDatas = provinceList.toArray(new String[provinceList.size()]);

            mSelectAreaDialog = createDialog(ownerActivity, R.layout.dialog_select_area_data, Gravity.CENTER);

            mProvince = (WheelView) mSelectAreaDialog.findViewById(R.id.id_province);
            mCity = (WheelView) mSelectAreaDialog.findViewById(R.id.id_city);
            mArea = (WheelView) mSelectAreaDialog.findViewById(R.id.id_area);

            mProvince.setVisibleItems(5);
            mCity.setVisibleItems(5);
            mArea.setVisibleItems(5);

            mProvince.addChangingListener(mOnWheelChangedListener);
            mCity.addChangingListener(mOnWheelChangedListener);
            mArea.addChangingListener(mOnWheelChangedListener);

            mSelectAreaDialog.findViewById(R.id.lb_btn_select).setOnClickListener(v -> {
                final List<DistrictDataModel> districtDataModels = mAreaDatasMap.get(mCurrentCityName);
                DistrictDataModel currentArea = new DistrictDataModel();
                if (districtDataModels != null)
                    currentArea = districtDataModels.get(mArea.getCurrentItem());

                String addr = mCurrentProviceName+" " + mCurrentCityName +" "+ currentArea.getAddress();
                String zipCode = currentArea.getZipCode();
                String id = currentArea.getId();
                Bundle resultData = new Bundle();
                mOnSelectAreaListener.onSelect(addr, zipCode, id);
                mSelectAreaDialog.hide();
            });
            mSelectAreaDialog.findViewById(R.id.lbcx_btn_cancel).setOnClickListener(v -> mSelectAreaDialog.hide());
            mProvince.setViewAdapter(new ArrayWheelAdapter<>(ownerActivity, mProvinceDatas));
        }

        /**
         * 根据当前的市，更新区WheelView的信息
         */
        private void updateAreas() {
            int pCurrent = mCity.getCurrentItem();
            final List<String> city = mCitisDatasMap.get(mCurrentProviceName);
            List<DistrictDataModel> areasList = null;
            if (city != null) {
                mCurrentCityName = city.get(pCurrent);
                areasList = mAreaDatasMap.get(mCurrentCityName);
            }

            if (areasList == null) {
                areasList = new ArrayList<>();
                areasList.add(new DistrictDataModel());
                mCurrentCityName = "";
            }
            mArea.setViewAdapter(new DistrictListWheelAdapter(ownerActivity, areasList));
            if (areasList.size() > 0) {
                mArea.setCurrentItem(0);
                mArea.setVisibility(View.VISIBLE);
            }
        }

        /**
         * 根据当前的省，更新市WheelView的信息
         */
        private void updateCities() {
            int pCurrent = mProvince.getCurrentItem();
            mCurrentProviceName = mProvinceDatas[pCurrent];
            List<String> citiesList = mCitisDatasMap.get(mCurrentProviceName);
            if (citiesList == null) {
                citiesList = new ArrayList<>(0);
                citiesList.add("");
            }
            mCity.setViewAdapter(new ListWheelAdapter<>(ownerActivity, citiesList));
            if (citiesList.size() > 0) {
                mCity.setCurrentItem(0);
                mCity.setVisibility(View.VISIBLE);
            }
            updateAreas();
        }
    }


}
